package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.Bundle;
import android.os.RemoteException;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.keldian.mobilesafe.R;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by Administrator on 16-1-25.
 */
public class lCleanCacheActivity extends Activity {
    private ProgressBar pb;
    private TextView tv_scan_status;
    private PackageManager pm;
    private LinearLayout ll_container;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clean_cache);
        tv_scan_status = (TextView) findViewById(R.id.tv_scan_status);
        pb = (ProgressBar) findViewById(R.id.pb);
        ll_container = (LinearLayout) findViewById(R.id.ll_container);
        scanCache();
    }

    private void scanCache() {
        pm = getPackageManager();
        new Thread() {
            @Override
            public void run() {
                Method getPackageSizeInfoMethod = null;
                Method[] methods = PackageManager.class.getMethods();
                for (Method method : methods) {
                    if ("getPackageSizeInfo".equals(method.getName())) {
                        getPackageSizeInfoMethod = method;
                    }
                }
                List<PackageInfo> infos = pm.getInstalledPackages(0);
                pb.setMax(infos.size());
                int progress = 0;
                for (PackageInfo info : infos) {
                    try {
                        getPackageSizeInfoMethod.invoke(pm, info.packageName, new MyDataObserver());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    progress++;
                    pb.setProgress(progress);
                }
            }
        }.start();

    }

    private class MyDataObserver extends IPackageStatsObserver.Stub {

        @Override
        public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
                throws RemoteException {
            final long cache = pStats.cacheSize;
            long code = pStats.codeSize;
            long data = pStats.dataSize;
            final String packname = pStats.packageName;
            final ApplicationInfo appInfo;
            try {
                appInfo = pm.getApplicationInfo(packname, 0);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
